/*
 * jxUtils Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.i18n;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxUtils
 * @package org.jxUtils.i18n
 * @date 21/01/2014 - 19:45:06
 */
public class Localizator {
	
	static final String PACKAGE_NAME = Localizator.class.getPackage().getName();
	
	private ResourceBundle resourceBundle = null;
	
	/**
	 * @param resourceBundle
	 */
	public Localizator(final ResourceBundle resourceBundle) {
	
		this.resourceBundle = resourceBundle;
	}
	
	/**
	 * @param resourceBundlePath
	 */
	public Localizator(final String resourceBundlePath) {
	
		this(ResourceBundle.getBundle(resourceBundlePath));
	}
	
	/**
	 * @param resourceBundlePath
	 * @param locale
	 */
	public Localizator(final String resourceBundlePath, final Locale locale) {
	
		try {
			this.resourceBundle = ResourceBundle.getBundle(resourceBundlePath, locale);
			
		}catch(MissingResourceException missingResourceException1) {
			
			try {
				this.resourceBundle = ResourceBundle.getBundle(resourceBundlePath);
				
			}catch(MissingResourceException missingResourceException2) {
				
				try {
					this.resourceBundle = ResourceBundle.getBundle(resourceBundlePath, Locale.ENGLISH);
					
				}catch(MissingResourceException missingResourceException3) {
					
					try {
						this.resourceBundle = ResourceBundle.getBundle(resourceBundlePath, new Locale("pt", "BR"));
						
					}catch(MissingResourceException missingResourceException4) {
						
						throw missingResourceException1;
					}
				}
			}
		}
	}
	
	/**
	 * @param key
	 * @param params
	 * @return the message built
	 */
	protected final String getMessage(final String key, final Object... params) {
	
		String message = getResourceBundle().getString(key);
		
		int i = 0;
		
		// 
		if( !isNullOrEmpty(params)) {
			
			//
			for(final Object param : params) {
				
				message = message.replace("{" + i + "}", String.valueOf(param));
				
				i++;
			}
		}
		
		message = message.replaceAll("[{]{1}[0-9]{1,}[}]{1}", "");
		
		return message;
	}
	
	/**
	 * @param key
	 * @return the object
	 */
	protected final Object getObject(final String key) {
	
		return getResourceBundle().getObject(key);
	}
	
	/**
	 * @return resourceBundle locale
	 */
	public final Locale getLocale() {
	
		return getResourceBundle().getLocale();
	}
	
	/**
	 * @return the resourceBundle
	 */
	public final ResourceBundle getResourceBundle() {
	
		return resourceBundle;
	}
	
	/**
	 * @param params
	 * @return <b>true</b> se <b>params</b> ou pelo menos 1 dos parâmetros estiver nulo ou vazio
	 */
	protected final static boolean isNullOrEmpty(final Object... params) {
	
		// 
		if(params != null && params.length > 0) {
			
			//
			for(final Object param : params) {
				
				//
				if(param == null) {
					
					return true;
					
				}else if(param instanceof String && ((String)param).trim().isEmpty()) {
					
					return true;
					
				}else {
					
					// Para qualquer tipo de Objeto
					if(param.toString().trim().isEmpty()) {
						
						return true;
					}
				}
			}
			
			return false;
			
		}else {
			
			return true;
		}
	}
}
